package org.sxp.common.dto;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.sxp.common.enums.DbTypeEnum;
import org.sxp.common.validator.group.*;

import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
import java.util.Date;

/**
 * 
 * 如果同时想发布到rpc，请拷贝至公共依赖模块，用于rpc之间数据交互
 * @author 沈兴平
 * @date 2021/12/30
 */
@Data
@ApiModel(value = "",description = "沈兴平-2021/12/30")
public class DatabaseDTO implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 * 主键
	 */
	@NotNull(message="id 不能为空", groups = {L2.class})
	@ApiModelProperty(value = "主键-主键")
	private String id;

	/**
	 * 数据源名称
	 */
	@ApiModelProperty(value = "数据源名称")
	private String name;

	/**
	 * 用户名
	 */
	@ApiModelProperty(value = "用户名")
	private String username;

	/**
	 * 密码
	 */
	@ApiModelProperty(value = "密码")
	private String password;

	/**
	 * 类型
	 */
	@ApiModelProperty(value = "类型")
	@NotBlank(message = "数据库类型不能为空", groups = {AddGroup.class, UpdateGroup.class})
	private String type;

	/**
	 * 连接地址
	 */
	@ApiModelProperty(value = "连接地址")
	@NotBlank(message = "数据库类型不能为空", groups = {MongodbGroup.class})
	private String url;
	/**
	 * 数据库名
	 */
	@ApiModelProperty(value = "数据库名")
	@NotBlank(message = "数据库名不能为空", groups = {MysqlGroup.class})
	@NotBlank(message = "服务名或者SID必填", groups = {OracleGroup.class})
	private String dbName;

	/**
	 * jdbc驱动类
	 */
	@ApiModelProperty(value = "jdbc驱动类")
	private String driverClassName;


	/**
	 * HadoopHdfs文件系统NameNode节点地址
	 */
	@ApiModelProperty(value = "HadoopHdfs文件系统NameNode节点地址")
	private String hdfsUrl;

	/**
	 * 描述
	 */
	@ApiModelProperty(value = "描述")
	private String remarks;

	/**
	 * 资源分组
	 */
	@ApiModelProperty(value = "资源分组")
	@NotBlank(message = "资源分组不能为空")
	private String spaceId;

	/**
	 * 集群名 - OceaBase
	 */
	@ApiModelProperty(value = "集群名")
	private String clusterName;
	/**
	 * 租户名 - OceaBase
	 */
	@ApiModelProperty(value = "租户名")
	private String tenantName;

	/**
	 * 是否公共数据库
	 */
	private Boolean isPub;

	/**
	 * 创建时间
	 */
	@ApiModelProperty(value = "创建时间")
	private Date createTime;

	/**
	 * 修改时间
	 */
	@ApiModelProperty(value = "修改时间")
	private Date updateTime;

	/**
	 * 创建人
	 */
	@ApiModelProperty(value = "创建人")
	private Long createUser;

	/**
	 * 修改人
	 */
	@ApiModelProperty(value = "修改人")
	private Long updateUser;


	@ApiModelProperty(value = "域名或IP")
	@NotBlank(message = "域名/IP不能为空", groups = {MysqlGroup.class, OracleGroup.class})
	private String host;
	@ApiModelProperty(value = "端口")
	@NotBlank(message = "端口不能为空", groups = {MysqlGroup.class, OracleGroup.class})
	private String port;

	private String oracleType;

	public void setUrl(String url) {
		this.url = url;
		if(StrUtil.isNotBlank(this.getUrl()) && this.getType().equals(DbTypeEnum.oracle.getCode())){
			if(this.getUrl().contains("//")){
				this.setOracleType("200");
			}else{
				this.setOracleType("201");
			}
		}
	}

	@ApiModelProperty(value = "Postgresql tableSchema")
	private String tableSchema;
}
